package com.ffzx.commerce.framework.utils;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

/**
 * 
 * @ClassName: DateTimeDeserializer
 * @Description: DateTime 反序列化 pattern= yyyy-MM-dd HH:mm:ss
 * @author 李淼淼 445052471@qq.com
 * @date 2016年6月2日 下午12:42:16
 * 用法
 * {@link JsonDeserialize}
 <pre>
 	@JsonDeserialize(using=DateTimeDeserializer.class)
 	private Date createTime
</pre>
 */

public class DateTimeDeserializer extends JsonDeserializer<Date> {

	@Override
	public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
		String text = jp.getText();
		if (StringUtils.isNoneBlank(text)) {
			try {
				return DateUtil.parse(text, "yyyy-MM-dd HH:mm:ss");
			} catch (Exception e) {
				// ignore
			}
		}
		return null;
	}

}
